#include "WSInputManager.h"

WSInputManager::WSInputManager()
{

}

WSInputManager::~WSInputManager()
{

}

void WSInputManager::Initialize()
{
	ZeroMemory(&mKeyboardState, 256);
	ZeroMemory(&mMouseState, sizeof(DIMOUSESTATE2));

	HINSTANCE hAppInst = ::GetModuleHandle(NULL);
	HWND hWnd = ::GetActiveWindow();

	DirectInput8Create(hAppInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void **) &mDirectInput, 0);

	mDirectInput->CreateDevice(GUID_SysKeyboard, &mKeyboardDevice, 0);
	mKeyboardDevice->SetDataFormat(&c_dfDIKeyboard);
	mKeyboardDevice->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
	mKeyboardDevice->Acquire();

	mDirectInput->CreateDevice(GUID_SysMouse, &mMouseDevice, 0);
	mMouseDevice->SetDataFormat(&c_dfDIMouse2);
	mMouseDevice->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
	mMouseDevice->Acquire();
}

void WSInputManager::Destroy()
{
	WS_SAFE_RELEASE(mDirectInput);

	mKeyboardDevice->Unacquire();
	WS_SAFE_RELEASE(mKeyboardDevice);

	mMouseDevice->Unacquire();
	WS_SAFE_RELEASE(mMouseDevice);
}

void WSInputManager::update()
{
	HRESULT hr = mKeyboardDevice->GetDeviceState(256, (void **) &mKeyboardState);
	if (FAILED(hr))
	{
		ZeroMemory(&mKeyboardState, 256);
		mKeyboardDevice->Acquire();
	}

	hr = mMouseDevice->GetDeviceState(sizeof(DIMOUSESTATE2), (void **) &mMouseState);
	if (FAILED(hr))
	{
		ZeroMemory(&mMouseState, sizeof(DIMOUSESTATE2));
		mMouseDevice->Acquire();
	}
}